﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using IOEntity;

namespace IODataAccess
{
    public class clsDASupport
    {
        clsDAConnection con = new clsDAConnection();
        public clsSupport GetById(int id)
        {
            try
            {
                clsSupport obj = new clsSupport();
                string str = "select * from tblSupport where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    obj.Id = id;
                    if (dr["name"] != DBNull.Value)
                    {
                        obj.Name = dr["name"].ToString();
                    }
                    if (dr["dienthoai"] != DBNull.Value)
                    {
                        obj.Dienthoai = dr["dienthoai"].ToString();
                    }
                    if (dr["nick"] != DBNull.Value)
                    {
                        obj.Nick = dr["nick"].ToString();
                    }
                }
                return obj;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public List<clsSupport> GetAll()
        {
            try
            {
                List<clsSupport> list = new List<clsSupport>();
                string str = "select * from tblSupport";
                SqlCommand com = new SqlCommand(str, con.getCon());
                con.openCon();
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    clsSupport obj = new clsSupport();
                    if (dr["name"] != DBNull.Value)
                    {
                        obj.Name = dr["name"].ToString();
                    }
                    if (dr["dienthoai"] != DBNull.Value)
                    {
                        obj.Dienthoai = dr["dienthoai"].ToString();
                    }
                    if (dr["nick"] != DBNull.Value)
                    {
                        obj.Nick = dr["nick"].ToString();
                    }
                    if (dr["id"] != DBNull.Value)
                    {
                        obj.Id = Convert.ToInt32(dr["id"].ToString());
                    }
                    list.Add(obj);
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public int Insert(clsSupport obj)
        {
            try
            {
                string str = "insert into tblsupport(name,dienthoai,nick) values(@name,@dienthoai,@nick)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@name", obj.Name);
                com.Parameters.AddWithValue("@dienthoai", obj.Dienthoai);
                com.Parameters.AddWithValue("@nick", obj.Nick);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
                return clsDAStaticMethod.getIDIdentity("tblsupport");
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Update(clsSupport obj)
        {
            try
            {
                string str = "Update tblsupport set name=@name,dienthoai=@dienthoai,nick=@nick where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@name", obj.Name);
                com.Parameters.AddWithValue("@dienthoai", obj.Dienthoai);
                com.Parameters.AddWithValue("@nick", obj.Nick);
                com.Parameters.AddWithValue("@id", obj.Id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Delete(int id)
        {
            try
            {
                string str = "delete from tblsupport where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
    }
}
